package com.es.elasticsearch.controller;


import com.es.elasticsearch.util.ESIndexUtil;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class ESIndexController {


    /**
     * 新增索引
     * @param indexName
     * @return
     * @throws IOException
     */
    @GetMapping("/es/index/add/{index}")
    public String add(@PathVariable(value = "index") String indexName) throws IOException {
        if (StringUtils.isBlank(indexName)) {
            return "Error : indexName不能为空";
        }

        return ESIndexUtil.createIndex(indexName);

    }

    /**
     * 查询索引
     * @param indexName
     * @return
     * @throws IOException
     */
    @GetMapping("/es/index/get/{index}")
    public String query(@PathVariable(value = "index") String indexName) throws IOException {
        if (StringUtils.isBlank(indexName)) {
            return "Error : indexName不能为空";
        }

        String queryIndex = ESIndexUtil.queryIndex(indexName);

        return queryIndex;
    }


    /**
     * 删除索引
     * @param indexName
     * @return
     * @throws IOException
     */
    @GetMapping("/es/index/delete/{index}")
    public String delete(@PathVariable(value = "index") String indexName) throws IOException {
        if (StringUtils.isBlank(indexName)) {
            return "Error : indexName不能为空";
        }


        return ESIndexUtil.deleteIndex(indexName);
    }

}
